# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
from tutorial import settings
class TutorialPipeline(object):
    def process_item(self, item, spider):
        return item


class DoubanPipeline(object):
    def __init__(self):
        self.file = open('douban_top250.txt',mode='w',encoding='utf-8')

    def process_item(self,item,spider):
        line = ''

        for i in range(1,len(item['rating'])-1):
            rank = item['rank']
            name = item['name']
            rating = item['rating']
            line = rank +'  '+name+' '+ rating +'\n'
        self.file.write(line)

    def close_spider(self,spider):
        self.file.close()

class DBPipeline(object):
    def __init__(self):
        # 连接数据库
        self.connect = pymysql.connect(
            host=settings.MYSQL_HOST,
            port=3306,
            db=settings.MYSQL_DBNAME,
            user=settings.MYSQL_USER,
            passwd=settings.MYSQL_PASSWD,
            charset='utf8',
            use_unicode=True)

        # 通过cursor执行增删查改
        self.cursor = self.connect.cursor()

    def process_item(self, item, spider):
        try:
            # 查重处理
            self.cursor.execute(
                """select * from doubanmovie where rank = %s""",
                item['rank'])
            # 是否有重复数据
            repetition = self.cursor.fetchone()

            # 重复
            if repetition:
                pass


            else:
                # 插入数据
                self.cursor.execute(
                    """insert into doubanmovie(rank, name, rating)
                    value (%s, %s, %s)""",
                    (item['rank'],
                     item['name'],
                     item['rating'],
                     ))

                # 提交sql语句
                self.connect.commit()

        except Exception as error:
            # 出现错误时打印错误日志
            print(error)
        return item